package com.zhiyou.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.dao.LogisticsMapper;
import com.zhiyou.dao.PrivilegeMapper;
import com.zhiyou.entity.Logistics;
import com.zhiyou.entity.Privilege;
import com.zhiyou.service.LogisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

//后勤人员管理
@Controller
@RequestMapping("/Logistics")
public class LogisticsController {
    @Autowired
    LogisticsService logisticsService;
    @RequestMapping("/getAll")
    public String getAll(Model model, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        //查询所有后勤人员
        PageHelper.startPage(pageNum,2);
        List<Logistics> logistics = logisticsService.getAllLogistics();
        PageInfo<Logistics> pageInfo=new PageInfo<>(logistics,5);
        model.addAttribute("logistics",logistics);
        model.addAttribute("pageInfo",pageInfo);
        return "logisticsList";
    }
    //模糊查询
    @RequestMapping("/getLogisticsLikeAll")
    public String getLogisticsLikeAll(Model model, @RequestParam(value = "likes") String likes){
        List<Logistics> logisticsLikeAll = logisticsService.getLogisticsLikeAll(likes);
        model.addAttribute("logistics",logisticsLikeAll);
        return "logisticsList";
    }

    //跳转新增界面
    @RequestMapping("/doAddLogistics")
    public String doAddLogistics(){
        return "logisticsAdd";
    }

    //新增操作
    @RequestMapping("/addLogistics")
    public String addLogistics(Logistics logistics){
        int i = logisticsService.addLogistics(logistics);
        if (i!=0){
            return "redirect:/Logistics/getAll";
        }
       return "logisticsError";
    }
    //删除操作
    @RequestMapping("/deleteLogistics")
    public String deleteLogistics(Integer id){
        int i = logisticsService.deleteLogistics(id);
        if (i!=0){
            return "redirect:/Logistics/getAll";
        }
        return "logisticsError";
    }

    //展示修改界面信息
    @RequestMapping("/toUpdateLogistics")
    public String toUpdateLogistics(Integer id,Model model){
        List<Logistics> logistics = logisticsService.selectLogistics(id);
        model.addAttribute("log",logistics);
        return "logisticsUpdate";
    }

    //修改操作
    @RequestMapping("/updateLogistics")
    public String UpdateLogistics(Logistics logistics,Model model){
        int i = logisticsService.updateLogistics(logistics);
        if (i!=0){
            return "redirect:/Logistics/getAll";
        }
        return "logisticsError";
    }


}
